<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" draggable overflow>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="90" label-position="right">
            <el-form-item>
                <div style="color: #f00;font-size: 16px;font-weight: 600" v-if="number != 0">
                    {{`单据中有${number}匹销售单价小于100,请检查!`}}
                </div>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="form.status" placeholder="状态" @change="statusChange">
                    <el-option label="挂单" :value="0"></el-option>
                    <el-option label="过账" :value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="应付总额" prop="contract_price" v-if="form.status == 1">
                <el-input v-model="contract_price" placeholder="应付总额" readonly></el-input>
            </el-form-item>
            <el-form-item label="对应账户" prop="account_id" v-if="form.status == 1">
                <el-select v-model="form.account_id" placeholder="请选择" filterable clearable>
                    <el-option v-for="item in accountList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="支付总额" prop="pay_price" v-if="form.status == 1">
                <!-- <el-input-number v-model="form.pay_price" :controls="false" placeholder="支付总额" @focus="common.inputFocus($event)" style="width: 100%;"></el-input-number> -->
                <el-input v-model="form.pay_price" placeholder="支付总额" type="number" @mousewheel.prevent @focus="common.inputFocus($event)" style="width: 100%;">
                    <template #append>{{common.formatNumber(form.pay_price, 0)}}</template>
                </el-input>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			dialog: {
				client: false
			},
			mode: "add",
			titleMap: {
				add: '设置状态',
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: [],
                status:1,
                account_id: "",
                pay_price: "",
			},
			//验证规则
			rules: {
                status: [
					{ required: true, message: '请选择通过拒绝' }
				],
            },
            accountList: [],
            contract_price: "",
            price: "",
            number: "",
		}
	},
	mounted() {
        this.getAccount();
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
            if(this.status == 1){
                if((this.form.account_id != '' && this.form.account_id != undefined) && this.form.pay_price == '') return this.$alert("请填写支付金额", "提示", {type: 'error'})
                if((this.form.account_id == '' || this.form.account_id == undefined) && this.form.pay_price != '') return this.$alert("请选择对应账户", "提示", {type: 'error'})
            }
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
                    var res = await this.$API.business.sale[this.form.status ? 'check' : 'uncheck'].post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
           this.form.id = data.id
           this.contract_price = data.contract_price
           this.form.status = data.status
           this.form.account_id = data.account_id ? data.account_id : ""
           this.form.pay_price = data.pay_number != 0 ? "" : data.pay_price
           this.number = data.no_price_count
		},
        // 获取账户
        async getAccount() {
            var res = await this.$API.basic.account.all.get();
            const array = []
            res.data.map(item=>{
                array.push({
                    id: item.id,
                    name: `${item.account}【${item.paytype?.paytype_name}】余额: ${item.balance}`
                })
            })
            this.accountList = array
        },
        statusChange(){
            this.form.account_id = ""
            this.form.pay_price = ""
        },
	}
}
</script>

<style lang="scss">
.el-input-group__append{
    padding: 0 8px;
    width: 130px;
}
</style>
